/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.controller;

import com.bayu.services.dao.K3Dao;
import com.bayu.services.entity.K3;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Bayu
 */
/**
 *
 * @author Bayu
 */
@Controller
@RequestMapping("/k3-input.htm")
public class K3Input {
    K3Dao k3Dao;

    @Autowired
    private void setK3Dao(K3Dao k3Dao){
      this.k3Dao = k3Dao;
    }

    @RequestMapping(method = RequestMethod.GET)
    public void displayForm(ModelMap map,String id){

    }

    @RequestMapping(method = RequestMethod.POST)
    public String proccessForm(@ModelAttribute("K3") K3 k3 , BindingResult result,ModelMap map, String date,HttpServletRequest req){
            DateFormat formarter = new SimpleDateFormat("yy-MM-dd");
            Date tgl;

            if(result.hasFieldErrors("date")){
                try {
                    tgl = formarter.parse(date);
                    k3.setDate(tgl);
                } catch (ParseException ex) {
                    Logger.getLogger(K3.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

               List total = this.k3Dao.custom("select count(*) from K3 where date='"+date+"'");

            String strTotal=total.get(0).toString();


            int c = Integer.valueOf(strTotal).intValue();
            System.out.println(strTotal+"---"+c+"--"+date);
            if(c > 0){
                return "redirect:ErrorDuplicate.html";
            }else{


                 this.k3Dao.save(k3);
                 return "redirect:k3-input.htm";
            }


    }
}

